<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderHgController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '换购订单管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order);

        $grid->model()->where('type',1);
        $grid->column('id', __('Id'));
        $grid->column('pay_type', __('支付方式'));
        $grid->column('order_no', __('订单号'));
        $grid->column('money', __('金额'));
        $grid->column('status', __('状态'));
        $grid->column('comment', __('备注'))->display(function ($value) {
            switch ($value) {
                case '1':
                    return "<span class='label label-success'>已通过</span>";
                    break;
                case '2':
                    return "<span class='label label-primary'>已取消</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->column('created_at', __('创建时间'));

        $grid->disableActions();
        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('Type'));
        $show->field('pay_type', __('Pay type'));
        $show->field('order_no', __('Order no'));
        $show->field('money', __('Money'));
        $show->field('status', __('Status'));
        $show->field('comment', __('Comment'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order);

        $form->switch('type', __('Type'));
        $form->switch('pay_type', __('Pay type'))->default(1);
        $form->text('order_no', __('Order no'));
        $form->decimal('money', __('Money'));
        $form->switch('status', __('Status'));
        $form->text('comment', __('Comment'));

        return $form;
    }
}
